<?php

class BonusController extends MemberController {

    public function actionSummary() {

        $summary = vo_network::get_bonus_acc(Yii::app()->user->getState('network_id'));
        $today = vo_network::get_bonus_by_netgrow(Yii::app()->user->getState('network_id'), date('Y-m-d'));
        $today['gen_node_bonus'] = Yii::app()->db->createCommand("SELECT (IFNULL(SUM(gen_node_netgrow_value), 0) * " . Yii::app()->params['bonus']['gen_node'] . ") AS gen_node_bonus FROM sp_gen_node_netgrow WHERE gen_node_netgrow_network_id = '" . Yii::app()->user->getState('network_id') . "' AND gen_node_netgrow_date = '" . date('Y-m-d') . "'")->queryScalar();
        $today['sponsor_upgrade_bonus'] = Yii::app()->db->createCommand("SELECT IFNULL(SUM(sponsor_upgrade_bonus_log_value), 0) AS sponsor_upgrade_bonus FROM sp_sponsor_upgrade_bonus_log WHERE sponsor_upgrade_bonus_log_network_id = '" . Yii::app()->user->getState('network_id') . "' AND sponsor_upgrade_bonus_log_date = '" . date('Y-m-d') . "'")->queryScalar();
        $today['match_bonus'] = Yii::app()->db->createCommand("SELECT (IFNULL(SUM(match_netgrow_value), 0) * " . Yii::app()->params['bonus']['match'] . ") AS match_bonus FROM sp_match_netgrow WHERE match_netgrow_network_id = '" . Yii::app()->user->getState('network_id') . "' AND match_netgrow_date = '" . date('Y-m-d') . "'")->queryScalar();
        $today['total_bonus'] += ($today['gen_node_bonus']+$today['match_bonus']+$today['sponsor_upgrade_bonus']);
        
        //$ewallet_income = vo_network::get_ewallet_income_by_date(Yii::app()->user->getState('network_id'), date('Y-m-d'));
        $ewallet_income = vo_network::get_ewallet_income(Yii::app()->user->getState('network_id'));
        $ewallet_cash = $ewallet_noncash = $ewallet_products = $ewallet_voucher = array(
            'sponsor' => 0,
            'extra_sponsor' => 0,
            'gen_node' => 0,
            'gen_node_products' => 0,
            'gen_node_extra' => 0,
            'gen_node_extra_warehouse' => 0,
            'match' => 0,
            'gen_match' => 0,
            'matrix_node' => 0,
            'board_level' => 0,
            'board_fly' => 0,
            'total_bonus' => 0,
            'sponsor_upgrade' => 0,
        );
        if ($ewallet_income != false) {
            foreach ($ewallet_income as $row_ewallet_income) {
                $ewallet_cash[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_cash'];
                $ewallet_noncash[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_noncash'];
                $ewallet_products[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_products'];
                $ewallet_voucher[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_voucher'];

                $ewallet_cash['total_bonus'] += $row_ewallet_income['ewallet_income_cash'];
                $ewallet_noncash['total_bonus'] += $row_ewallet_income['ewallet_income_noncash'];
                $ewallet_products['total_bonus'] += $row_ewallet_income['ewallet_income_products'];
                $ewallet_voucher['total_bonus'] += $row_ewallet_income['ewallet_income_voucher'];
            }
        }

        
        $this->render('summary', array(
            'summary' => $summary,
            'today' => $today,
            'ewallet_cash' => $ewallet_cash,
            'ewallet_noncash' => $ewallet_noncash,
            'ewallet_products' => $ewallet_products,
            'ewallet_voucher' => $ewallet_voucher,
        ));
    }

    public function actionLog() {
        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;

        $date_diff = ((strtotime($date_end) - strtotime($date_start)) / 86400) + 1;
        $network_id = Yii::app()->user->getState('network_id');

        $data = array();

        $total = array(
            'sponsor' => 0,
            'extra_sponsor' => 0,
            'gen_node' => 0,
            'gen_node_products' => 0,
            'gen_node_extra' => 0,
            'gen_node_extra_warehouse' => 0,
            'match' => 0,
            'gen_match' => 0,
            'matrix_node' => 0,
            'board_level' => 0,
            'board_fly' => 0,
            'total_bonus' => 0,
        );
        for ($i = 0; $i < $date_diff; $i++) {
            $arr_date = explode("-", $date_start);
            $date = date("Y-m-d", mktime(0, 0, 0, $arr_date[1], $arr_date[2] + $i, $arr_date[0]));

            $sponsor = function_lib::get_one("sponsor_bonus_log_value", "sp_sponsor_bonus_log", "sponsor_bonus_log_network_id = '" . $network_id . "' AND sponsor_bonus_log_date = '" . $date . "'");
            $extra_sponsor = function_lib::get_one("extra_sponsor_bonus_log_value", "sp_extra_sponsor_bonus_log", "extra_sponsor_bonus_log_network_id = '" . $network_id . "' AND extra_sponsor_bonus_log_date = '" . $date . "'");
            $gen_node = function_lib::get_one("SUM(gen_node_bonus_log_value)", "sp_gen_node_bonus_log", "gen_node_bonus_log_network_id = '" . $network_id . "' AND gen_node_bonus_log_date = '" . $date . "'");
            $gen_node_products = function_lib::get_one("SUM(gen_node_products_bonus_log_value)", "sp_gen_node_products_bonus_log", "gen_node_products_bonus_log_network_id = '" . $network_id . "' AND gen_node_products_bonus_log_date = '" . $date . "'");
            $gen_node_extra = function_lib::get_one("SUM(gen_node_extra_bonus_log_value)", "sp_gen_node_extra_bonus_log", "gen_node_extra_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_bonus_log_date = '" . $date . "'");
            $gen_node_extra_warehouse = function_lib::get_one("SUM(gen_node_extra_warehouse_bonus_log_value)", "sp_gen_node_extra_warehouse_bonus_log", "gen_node_extra_warehouse_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_warehouse_bonus_log_date = '" . $date . "'");
            $match = function_lib::get_one("match_bonus_log_value", "sp_match_bonus_log", "match_bonus_log_network_id = '" . $network_id . "' AND match_bonus_log_date = '" . $date . "'");
            $gen_match = function_lib::get_one("SUM(gen_match_bonus_log_value)", "sp_gen_match_bonus_log", "gen_match_bonus_log_network_id = '" . $network_id . "' AND gen_match_bonus_log_date = '" . $date . "'");
            $matrix_node = function_lib::get_one("SUM(matrix_node_bonus_log_value)", "sp_matrix_node_bonus_log", "matrix_node_bonus_log_network_id = '" . $network_id . "' AND matrix_node_bonus_log_date = '" . $date . "'");
            $board_level = function_lib::get_one("SUM(board_level_bonus_log_value)", "sp_board_level_bonus_log", "board_level_bonus_log_network_id = '" . $network_id . "' AND board_level_bonus_log_date = '" . $date . "'");
            $board_fly = function_lib::get_one("SUM(board_fly_bonus_log_value)", "sp_board_fly_bonus_log", "board_fly_bonus_log_network_id = '" . $network_id . "' AND board_fly_bonus_log_date = '" . $date . "'");
            $sponsor_upgrade= function_lib::get_one("SUM(sponsor_upgrade_bonus_log_value)", "sp_sponsor_upgrade_bonus_log", "sponsor_upgrade_bonus_log_network_id = '" . $network_id . "' AND sponsor_upgrade_bonus_log_date = '" . $date . "'");
            $total_bonus = $gen_node_extra_warehouse+$sponsor + $extra_sponsor + $gen_node + $match + $gen_match + $matrix_node + $board_level + $board_fly + $gen_node_extra + $gen_node_products + $sponsor_upgrade;

            if ($total_bonus > 0) {
                $data[$i]['date'] = $date;
                $data[$i]['sponsor'] = $sponsor;
                $data[$i]['extra_sponsor'] = $extra_sponsor;
                $data[$i]['gen_node'] = $gen_node;
                $data[$i]['gen_node_products'] = $gen_node_products;
                $data[$i]['gen_node_extra'] = $gen_node_extra;
                $data[$i]['gen_node_extra_warehouse'] = $gen_node_extra_warehouse;
                $data[$i]['match'] = $match;
                $data[$i]['gen_match'] = $gen_match;
                $data[$i]['matrix_node'] = $matrix_node;
                $data[$i]['board_level'] = $board_level;
                $data[$i]['board_fly'] = $board_fly;
                $data[$i]['sponsor_upgrade'] = $sponsor_upgrade;
                $data[$i]['total_bonus'] = $total_bonus;

                $total['sponsor'] += $sponsor;
                $total['extra_sponsor'] += $extra_sponsor;
                $total['gen_node'] += $gen_node;
                $total['gen_node_products'] += $gen_node_products;
                $total['gen_node_extra'] += $gen_node_extra;
                $total['gen_node_extra_warehouse'] += $gen_node_extra_warehouse;
                $total['match'] += $match;
                $total['gen_match'] += $gen_match;
                $total['matrix_node'] += $matrix_node;
                $total['board_level'] += $board_level;
                $total['board_fly'] += $board_fly;
                $total['sponsor_upgrade'] += $sponsor_upgrade;
                $total['total_bonus'] += $total_bonus;
            }
        }

        $this->render('log', array(
            'date_start' => $date_start,
            'date_end' => $date_end,
            'date_diff' => $date_diff,
            'data' => $data,
            'total' => $total,
        ));
    }

    public function actionPrestasi_distributor() {

        $data = vo_network::get_bonus_gen_node(Yii::app()->user->getState('network_id'));

        $this->render('detail_gen_node', array(
            'data' => $data,
        ));
    }

    public function actionPrestasi_factory() {

        $data = vo_network::get_bonus_gen_node_extra(Yii::app()->user->getState('network_id'));

        $this->render('detail_gen_node_extra', array(
            'data' => $data,
        ));
    }
    public function actionPrestasi_warehouse() {

        $data = vo_network::get_bonus_gen_node_extra_warehouse(Yii::app()->user->getState('network_id'));

        $this->render('detail_gen_node_extra_warehouse', array(
            'data' => $data,
        ));
    }

    public function actionRoyalti_produk() {

        $data = vo_network::get_bonus_gen_node_products(Yii::app()->user->getState('network_id'));

        $this->render('detail_gen_node_products', array(
            'data' => $data,
        ));
    }
    
    public function actionRoyalti_angsuran() {

        $bulan = function_lib::uri_segment(4);
        $current_month = date('m');
        $periode['year'] = date("Y");
        $periode['month'] = (!empty($bulan)) ? $bulan : $current_month;
        //$periode['month'] = "2";
        
        $periode['month_prev'] = ($periode['month']-1 > 1 ) ? $periode['month']-1 : 1 ;
        $periode['month_next'] = ($periode['month']+1 <= 12 ) ? $periode['month']+1 : $periode['month'] ;
        //die('dada');
        $data = vo_network::get_bonus_royalti_angsuran(Yii::app()->user->getState('network_id'),$periode);

        $this->render('detail_angsuran', array(
            'data' => $data,
            'periode' =>$periode, 
        ));
    }

    public function actionBoard_level() {

        $data_manager = vo_network::get_bonus_board_level(Yii::app()->user->getState('network_id'), 1);
        $data_director = vo_network::get_bonus_board_level(Yii::app()->user->getState('network_id'), 2);
        $total_bonus = $data_manager + $data_director;

        $this->render('detail_board_level', array(
            'data_manager' => $data_manager,
            'data_director' => $data_director,
            'total_bonus' => $total_bonus,
        ));
    }

    public function actionBoard_cycle() {

        $data_manager = vo_network::get_bonus_board_fly(Yii::app()->user->getState('network_id'), 1);
        $data_director = vo_network::get_bonus_board_fly(Yii::app()->user->getState('network_id'), 2);
        $total_bonus = $data_manager + $data_director;

        $this->render('detail_board_fly', array(
            'data_manager' => $data_manager,
            'data_director' => $data_director,
            'total_bonus' => $total_bonus,
        ));
    }

    public function actionStatement() {

        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;

        $date_diff = ((strtotime($date_end) - strtotime($date_start)) / 86400) + 1;
        $network_id = Yii::app()->user->getState('network_id');

        $data = array();

        $total = array(
            'bonus' => 0,
            'index' => 0,
            'subtotal' => 0,
            'adm' => 0,
            'total' => 0,
        );
        for ($i = 0; $i < $date_diff; $i++) {
            $arr_date = explode("-", $date_start);
            $date = date("Y-m-d", mktime(0, 0, 0, $arr_date[1], $arr_date[2] + $i, $arr_date[0]));
            $rs_bonus_cut_percent = Yii::app()->db->createCommand("SELECT * FROM sys_bonus_cut_daily WHERE bonus_cut_daily_date = '" . $date . "'")->queryRow();
            $index_percent = $rs_bonus_cut_percent['bonus_cut_daily_index'];
            $adm_percent = $rs_bonus_cut_percent['bonus_cut_daily_adm'];

            $sponsor = function_lib::get_one("sponsor_bonus_log_value", "sp_sponsor_bonus_log", "sponsor_bonus_log_network_id = '" . $network_id . "' AND sponsor_bonus_log_date = '" . $date . "'");
            $extra_sponsor = function_lib::get_one("extra_sponsor_bonus_log_value", "sp_extra_sponsor_bonus_log", "extra_sponsor_bonus_log_network_id = '" . $network_id . "' AND extra_sponsor_bonus_log_date = '" . $date . "'");
            $gen_node = function_lib::get_one("SUM(gen_node_bonus_log_value)", "sp_gen_node_bonus_log", "gen_node_bonus_log_network_id = '" . $network_id . "' AND gen_node_bonus_log_date = '" . $date . "'");
            $gen_node_products = function_lib::get_one("SUM(gen_node_products_bonus_log_value)", "sp_gen_node_products_bonus_log", "gen_node_products_bonus_log_network_id = '" . $network_id . "' AND gen_node_products_bonus_log_date = '" . $date . "'");
            $gen_node_extra = function_lib::get_one("SUM(gen_node_extra_bonus_log_value)", "sp_gen_node_extra_bonus_log", "gen_node_extra_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_bonus_log_date = '" . $date . "'");
            $gen_node_extra_warehouse = function_lib::get_one("SUM(gen_node_extra_warehouse_bonus_log_value)", "sp_gen_node_extra_warehouse_bonus_log", "gen_node_extra_warehouse_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_warehouse_bonus_log_date = '" . $date . "'");
            $match = function_lib::get_one("match_bonus_log_value", "sp_match_bonus_log", "match_bonus_log_network_id = '" . $network_id . "' AND match_bonus_log_date = '" . $date . "'");
            $gen_match = function_lib::get_one("SUM(gen_match_bonus_log_value)", "sp_gen_match_bonus_log", "gen_match_bonus_log_network_id = '" . $network_id . "' AND gen_match_bonus_log_date = '" . $date . "'");
            $matrix_node = function_lib::get_one("SUM(matrix_node_bonus_log_value)", "sp_matrix_node_bonus_log", "matrix_node_bonus_log_network_id = '" . $network_id . "' AND matrix_node_bonus_log_date = '" . $date . "'");
            $board_level = function_lib::get_one("SUM(board_level_bonus_log_value)", "sp_board_level_bonus_log", "board_level_bonus_log_network_id = '" . $network_id . "' AND board_level_bonus_log_date = '" . $date . "'");
            $board_fly = function_lib::get_one("SUM(board_fly_bonus_log_value)", "sp_board_fly_bonus_log", "board_fly_bonus_log_network_id = '" . $network_id . "' AND board_fly_bonus_log_date = '" . $date . "'");
            $sponsor_upgrade = function_lib::get_one("SUM(sponsor_upgrade_bonus_log_value)", "sp_sponsor_upgrade_bonus_log", "sponsor_upgrade_bonus_log_network_id = '" . $network_id . "' AND sponsor_upgrade_bonus_log_date = '" . $date . "'");
            $bonus = $gen_node_extra_warehouse + $sponsor + $extra_sponsor + $gen_node + $match + $gen_match + $matrix_node + $board_level + $board_fly + $gen_node_products + $gen_node_extra + $sponsor_upgrade;
            $index = Yii::app()->db->createCommand("SELECT SUM(ewallet_cut_index) AS cut_index FROM sys_ewallet_cut WHERE ewallet_cut_network_id = '" . $network_id . "' AND ewallet_cut_date = '" . $date . "'")->queryScalar();
            $subtotal = $bonus - $index;
            $adm = Yii::app()->db->createCommand("SELECT SUM(ewallet_cut_adm) AS cut_adm FROM sys_ewallet_cut WHERE ewallet_cut_network_id = '" . $network_id . "' AND ewallet_cut_date = '" . $date . "'")->queryScalar();
            $total_transfer = $subtotal - $adm;

            if ($bonus > 0) {
                $data[$i]['date'] = $date;
                $data[$i]['bonus'] = $bonus;
                $data[$i]['index_percent'] = $index_percent;
                $data[$i]['index'] = $index;
                $data[$i]['subtotal'] = $subtotal;
                $data[$i]['adm_percent'] = $adm_percent;
                $data[$i]['adm'] = $adm;
                $data[$i]['total'] = $total_transfer;

                $total['bonus'] += $bonus;
                $total['index'] += $index;
                $total['subtotal'] += $subtotal;
                $total['adm'] += $adm;
                $total['total'] += $total_transfer;
            }
        }

        $this->render('statement', array(
            'date_start' => $date_start,
            'date_end' => $date_end,
            'date_diff' => $date_diff,
            'data' => $data,
            'total' => $total,
        ));
    }

    public function actionStatement_detail() {
        $date = function_lib::uri_segment(4);
        $data['date'] = $date;

        $bonus = array(
            'sponsor' => 0,
            'extra_sponsor' => 0,
            'gen_node' => 0,
            'gen_node_products' => 0,
            'gen_node_extra' => 0,
            'gen_node_extra_waehouse' => 0,
            'match' => 0,
            'gen_match' => 0,
            'matrix_node' => 0,
            'board_level' => 0,
            'board_fly' => 0,
            'sponsor_upgrade' => 0,
            'total' => 0,
        );
        $cut_index = $cut_adm = $subtotal = array(
            'sponsor' => 0,
            'extra_sponsor' => 0,
            'gen_node' => 0,
            'gen_node_products' => 0,
            'gen_node_extra' => 0,
            'gen_node_extra_waehouse' => 0,
            'match' => 0,
            'gen_match' => 0,
            'matrix_node' => 0,
            'board_level' => 0,
            'board_fly' => 0,
            'sponsor_upgrade' => 0,
        );
        $total = array(
            'sponsor' => 0,
            'extra_sponsor' => 0,
            'gen_node' => 0,
            'gen_node_products' => 0,
            'gen_node_extra' => 0,
            'gen_node_extra_waehouse' => 0,
            'match' => 0,
            'gen_match' => 0,
            'matrix_node' => 0,
            'board_level' => 0,
            'board_fly' => 0,
            'sponsor_upgrade' => 0,
            'cut_index' => 0,
            'subtotal' => 0,
            'cut_adm' => 0,
            'total' => 0,
        );
        $network_id = Yii::app()->user->getState('network_id');
        $rs_bonus_cut_percent = Yii::app()->db->createCommand("SELECT * FROM sys_bonus_cut_daily WHERE bonus_cut_daily_date = '" . $date . "'")->queryRow();
        $percent['index'] = $rs_bonus_cut_percent['bonus_cut_daily_index'];
        $percent['adm'] = $rs_bonus_cut_percent['bonus_cut_daily_adm'];

        $bonus['sponsor'] = function_lib::get_one("sponsor_bonus_log_value", "sp_sponsor_bonus_log", "sponsor_bonus_log_network_id = '" . $network_id . "' AND sponsor_bonus_log_date = '" . $date . "'");
        $bonus['extra_sponsor'] = function_lib::get_one("extra_sponsor_bonus_log_value", "sp_extra_sponsor_bonus_log", "extra_sponsor_bonus_log_network_id = '" . $network_id . "' AND extra_sponsor_bonus_log_date = '" . $date . "'");
        $bonus['gen_node'] = function_lib::get_one("SUM(gen_node_bonus_log_value)", "sp_gen_node_bonus_log", "gen_node_bonus_log_network_id = '" . $network_id . "' AND gen_node_bonus_log_date = '" . $date . "'");
        $bonus['gen_node_products'] = function_lib::get_one("SUM(gen_node_products_bonus_log_value)", "sp_gen_node_products_bonus_log", "gen_node_products_bonus_log_network_id = '" . $network_id . "' AND gen_node_products_bonus_log_date = '" . $date . "'");
        $bonus['gen_node_extra'] = function_lib::get_one("SUM(gen_node_extra_bonus_log_value)", "sp_gen_node_extra_bonus_log", "gen_node_extra_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_bonus_log_date = '" . $date . "'");
        $bonus['gen_node_extra_warehouse'] = function_lib::get_one("SUM(gen_node_extra_warehouse_bonus_log_value)", "sp_gen_node_extra_warehouse_bonus_log", "gen_node_extra_warehouse_bonus_log_network_id = '" . $network_id . "' AND gen_node_extra_warehouse_bonus_log_date = '" . $date . "'");
        $bonus['match'] = function_lib::get_one("match_bonus_log_value", "sp_match_bonus_log", "match_bonus_log_network_id = '" . $network_id . "' AND match_bonus_log_date = '" . $date . "'");
        $bonus['gen_match'] = function_lib::get_one("SUM(gen_match_bonus_log_value)", "sp_gen_match_bonus_log", "gen_match_bonus_log_network_id = '" . $network_id . "' AND gen_match_bonus_log_date = '" . $date . "'");
        $bonus['matrix_node'] = function_lib::get_one("SUM(matrix_node_bonus_log_value)", "sp_matrix_node_bonus_log", "matrix_node_bonus_log_network_id = '" . $network_id . "' AND matrix_node_bonus_log_date = '" . $date . "'");
        $bonus['board_level'] = function_lib::get_one("SUM(board_level_bonus_log_value)", "sp_board_level_bonus_log", "board_level_bonus_log_network_id = '" . $network_id . "' AND board_level_bonus_log_date = '" . $date . "'");
        $bonus['board_fly'] = function_lib::get_one("SUM(board_fly_bonus_log_value)", "sp_board_fly_bonus_log", "board_fly_bonus_log_network_id = '" . $network_id . "' AND board_fly_bonus_log_date = '" . $date . "'");
        $bonus['sponsor_upgrade'] = function_lib::get_one("SUM(sponsor_upgrade_bonus_log_value)", "sp_sponsor_upgrade_bonus_log", "sponsor_upgrade_bonus_log_network_id = '" . $network_id . "' AND sponsor_upgrade_bonus_log_date = '" . $date . "'");
        $total['bonus'] = $bonus['gen_node_extra_warehouse'] + $bonus['sponsor'] + $bonus['extra_sponsor'] + $bonus['gen_node'] + $bonus['match'] + $bonus['gen_match'] + $bonus['matrix_node'] + $bonus['board_level'] + $bonus['board_fly'] + $bonus['sponsor_upgrade'];

        $sql_bonus_cut = "SELECT 
                    DISTINCT(ewallet_cut_bonus_name) AS bonus_name, 
                    SUM(ewallet_cut_index) AS cut_index, 
                    SUM(ewallet_cut_adm) AS cut_adm 
                    FROM sys_ewallet_cut 
                    WHERE ewallet_cut_network_id = '" . $network_id . "' 
                    AND ewallet_cut_date = '" . $date . "'
                    GROUP BY ewallet_cut_bonus_name";
        $rs_bonus_cut = Yii::app()->db->createCommand($sql_bonus_cut)->queryAll();
        if ($rs_bonus_cut != false) {
            foreach ($rs_bonus_cut as $row) {
                $cut_index[$row['bonus_name']] = $row['cut_index'];
                $subtotal[$row['bonus_name']] = $bonus[$row['bonus_name']] - $row['cut_index'];
                $cut_adm[$row['bonus_name']] = $row['cut_adm'];
                $total[$row['bonus_name']] = $subtotal[$row['bonus_name']] - $row['cut_adm'];
                $total['cut_index'] += $row['cut_index'];
                $total['subtotal'] += $subtotal[$row['bonus_name']];
                $total['cut_adm'] += $row['cut_adm'];
                $total['total'] += $total[$row['bonus_name']];
            }
        }

        $this->render('statement_detail', array(
            'data' => $data,
            'percent' => $percent,
            'bonus' => $bonus,
            'cut_index' => $cut_index,
            'cut_adm' => $cut_adm,
            'subtotal' => $subtotal,
            'total' => $total,
        ));
    }
    
    public function actionOmset(){
        //cari data
        $data_turnover = vo_network::get_turnover(Yii::app()->user->getState('network_id'));
        
        foreach ($data_turnover as $row){
            $data[$row['leadership_monthly_turnover_periode']]['date'] = $row['leadership_monthly_turnover_periode'];
            $data[$row['leadership_monthly_turnover_periode']]['value'] = $row['leadership_monthly_turnover_value'];
            
            $data_group = vo_network::get_turnover_group(Yii::app()->user->getState('network_id'),$row['leadership_monthly_turnover_periode']);
            foreach ($data_group as $row_group){
                $data[$row['leadership_monthly_turnover_periode']]['group'][] = $row_group['leadership_monthly_turnover_value'];
            }
        }
        
        $this->render('peringkat', array(
            'data' => $data,
        ));
    }

}
